package com.coffee.admin.controller.platform;

import com.alibaba.fastjson.JSONObject;
import com.coffee.admin.api.platform.query.PlatformRoleQuery;
import com.coffee.admin.api.platform.rep.PlatformOperateRes;
import com.coffee.admin.api.platform.req.PlatformAccountAddReq;
import com.coffee.admin.api.platform.req.PlatformAccountListReq;
import com.coffee.admin.api.platform.req.PlatformAccountUpdateReq;
import com.coffee.admin.api.platform.result.PlatformRoleResult;
import com.coffee.admin.api.platform.service.PlatformRoleService;
import com.coffee.admin.api.platform.service.PlatformService;
import com.coffee.admin.controller.BaseController;
import com.coffee.core.anotation.OperLog;
import com.coffee.core.anotation.PlatformLoginCheck;
import com.coffee.core.entity.LoginUser;
import com.coffee.core.entity.Page;
import com.coffee.core.entity.PagerInfo;
import com.coffee.core.exception.BusinessException;
import com.coffee.core.result.Result;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@RestController
@RequestMapping("/platform/account")
public class PlatformAccountController extends BaseController {

    @DubboReference(check = false)
    private PlatformRoleService platformRoleService;

    @DubboReference(check = false)
    private PlatformService platformService;

    @OperLog(operModul = "平台-系统管理-账户管理", operDesc = "分页查询账户管理列表")
    @PlatformLoginCheck(alias = "PlatformAccountListAlias")
    @RequestMapping(value = "/findList", method = {RequestMethod.POST})
    public Result<Page<PlatformOperateRes>> findList(HttpServletRequest request, @RequestBody PlatformAccountListReq params) throws BusinessException {
        Result<Page<PlatformOperateRes>> result = Result.success(null);
        PagerInfo pagerInfo = setDefaultPageInfo(params.getPageSize(), params.getPageIndex());
        result.setData(platformService.findMerchantOperatorListByPage(params, pagerInfo));
        return result;
    }

    @OperLog(operModul = "平台-系统设置-账户管理", operDesc = "获取账户详情")
    @PlatformLoginCheck(alias = "PlatformAccountListAlias")
    @RequestMapping(value = "/detail")
    public Result<PlatformOperateRes> detail(@RequestBody PlatformAccountListReq params) {
        Result<PlatformOperateRes> result = Result.success(null);
        result.setData(platformService.findMerchantOperatorDetail(params));
        return result;
    }

    @OperLog(operModul = "平台-系统管理-账户管理", operDesc = "新增账户")
    @PlatformLoginCheck(alias = "PlatformAccountOperationAlias")
    @RequestMapping(value = "/add", method = {RequestMethod.POST})
    public Result<String> save(@RequestBody PlatformAccountAddReq params) throws BusinessException {
        Result<String> result = Result.success(null);
        LoginUser supplier = getSupplier();
        params.setPlatformId(supplier.getPlatformId());
        platformService.insertPlatformOperate(params);
        return result;
    }

    @OperLog(operModul = "平台-系统管理-账户管理", operDesc = "修改账户")
    @PlatformLoginCheck(alias = "PlatformAccountOperationAlias")
    @RequestMapping(value = "/update", method = {RequestMethod.POST})
    public Result<String> update(@RequestBody PlatformAccountUpdateReq params) throws BusinessException {
        Result<String> result = Result.success(null);
        platformService.updatePlatformOperate(params);
        return result;
    }

    @OperLog(operModul = "平台-系统管理-账户管理", operDesc = "启用禁用登录账户")
    @PlatformLoginCheck(alias = "PlatformAccountOperationAlias")
    @RequestMapping(value = "/updateIsEnable", method = {RequestMethod.POST})
    public Result<String> updateIsEnable(@RequestBody JSONObject json) throws BusinessException {
        Result<String> result = Result.success(null);

        Integer id = json.getInteger("id");
        String isEnable = json.getString("isEnable");
        platformService.updateIsEnable(id, isEnable);
        return result;
    }

    @OperLog(operModul = "平台-系统管理-账户管理", operDesc = "删除账户")
    @PlatformLoginCheck(alias = "PlatformAccountOperationAlias")
    @RequestMapping(value = "/delete", method = {RequestMethod.POST})
    public Result<String> deleteOperate(@RequestBody JSONObject json) throws BusinessException {
        Result<String> result = Result.success(null);
        Integer id = json.getInteger("id");
        platformService.delete(id);
        return result;
    }

    @OperLog(operModul = "平台-系统管理-账户管理", operDesc = "查询角色列表")
    @PlatformLoginCheck(alias = "PlatformAccountListAlias")
    @RequestMapping(value = "/findRoleList", method = {RequestMethod.POST})
    public Result<List<PlatformRoleResult>> findRoleList(HttpServletRequest request) {
        Result<List<PlatformRoleResult>> result = Result.success(null);
        PlatformRoleQuery param = new PlatformRoleQuery();
        param.setIsAdmin("N");
        result.setData(platformRoleService.getList(param));
        return result;
    }
}
